package com.cn.demo4.dao.impl;

import com.cn.demo4.dao.OnlineDao;
import com.cn.demo4.model.OnlineModel;
import com.cn.demo4.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OnlineDaoImpl implements OnlineDao {
    @Override
    public List<OnlineModel> findAll() {
        String sql = "select * from online";
        ResultSet resultSet = JDBC.select(sql);
        return getData(resultSet);
    }

    @Override
    public List<OnlineModel> getOnlineModelListForHome() {
        String sql = "select * from online where status=1";
        ResultSet resultSet = JDBC.select(sql);
        return getData(resultSet);
    }

    private List<OnlineModel> getData(ResultSet resultSet) {
        List<OnlineModel> list = new ArrayList<>();
        try{
            while(resultSet.next()){
            OnlineModel onlineModel = new OnlineModel();
            onlineModel.setContent(resultSet.getString("content"));
            onlineModel.setStatus(resultSet.getInt("status"));
            onlineModel.setCreateTime(resultSet.getString("create_time"));
            onlineModel.setUpdateTime(resultSet.getString("update_time"));
            list.add(onlineModel);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
